package com.example.demo.day.config.learn.designBook.part8factory.part6;

import com.example.demo.day.config.learn.designBook.part8factory.part2.ConcreteProduct1;
import com.example.demo.day.config.learn.designBook.part8factory.part2.ConcreteProduct2;
import com.example.demo.day.config.learn.designBook.part8factory.part2.Product;

import java.util.HashMap;
import java.util.Map;

//延迟加载的工厂类
public class ProductFactory {

    private static final Map<String, Product> prMap = new HashMap<>();

    public static synchronized Product createProduct(String type) throws Exception {
        Product product = null;
        //如果prMap中已经有这个对象
        if (prMap.containsKey(type)) {
            product = prMap.get(type);
        } else {
            if (type.equals("Product1")) {
                product = new ConcreteProduct1();
            } else {
                product = new ConcreteProduct2();
            }
            prMap.put(type, product);
        }
        return product;


    }
}
